<template>
	<view>

		<form @submit="editSubmit">
			<view class="handleList_box">
				<view class="handleList_con">
					<view class="handleLabel handleLabelLong">工地名称：</view>
					<view class="handleList">
						<view class="pickerCon">
							<input type="text" name="name"  :value="worksiteDetail.name" placeholder="请填写工地名称"
								placeholder-class="placeholderColor">
						</view>
					</view>
				</view>
				<view class="handleList_con">
					<view class="handleLabel handleLabelLong">工地地址：</view>
					<view class="handleList">
						<view class="pickerCon">
							<input type="decimal" disabled :value="worksiteDetail.address" placeholder="请选择工地地址"
								placeholder-class="placeholderColor">
							<u-icon name="map-fill" color="#999" size="40" @click="getLocation"></u-icon>
						</view>
					</view>
				</view>
				<view class="handleList_con" v-if="worksiteDetail.boss.length>0">
					<view class="handleLabel handleLabelLong">工地老板：</view>
					<view class="handleList">
						<view class="pickerCon">
							{{worksiteDetail.boss[0]}}
						</view>
					</view>
				</view>
				<view class="handleList_con">
					<view class="handleLabel handleLabelLong">在职工友：</view>
					<view class="handleList">
						<view class="pickerCon">
							{{worksiteDetail.people_num}}
						</view>
					</view>
				</view>
			</view>

			<u-gap height="20" bg-color="#F5F5F5"></u-gap>

			<view class="setList_con">
				<!-- <view class="setItem">
					<image src="/static/wechat.png" mode="" class="setIcon"></image>
					<view class="setTit">关注公众号接收消息提醒</view>
					<u-switch v-model="receiveChecked" active-color="#327FE8" @change="receiveChange"></u-switch>
				</view> -->
				<view class="setItem">
					<image src="/static/projectIcon2.png" mode="" class="setIcon"></image>
					<view class="setTit">设为默认工地</view>
					<u-switch v-model="defaultChecked" active-color="#327FE8" @change="defaultChange"></u-switch>
				</view>
			</view>

			<view class="handlePost_con handleFixed">
				<view class="delBtn" @click="openDelShow">删除</view>
				<button form-type="submit" hover-class="none" class="completeBtn">修改</button>
			</view>
		</form>

	

		<!-- 删除工地弹窗 -->
		<u-mask :show="delShow">
			<view class="delPop_box">
				<view class="delPop_con">
					<form @submit="delSubmit">
						<view class="form_box">
							<!-- <view class="formIcon">
								<image src="/static/img/waringIcon.png" mode=""></image>
							</view> -->
							<view class="formTit">确定删除该项目吗？</view>
							<view class="formWarn">将会失去</view>
							<view class="formClass">
								<view class="classItem">
									记工记录
								</view>
								<view class="classItem">记账记录</view>
							</view>
							<view class="formDel">
								删除验证码【{{project_id}}】确认删除
							</view>
							<view class="controls">
								<input type="number" name="checkCard" placeholder='请输入删除验证码'
									placeholder-class="placeholderColor" />
							</view>
						</view>
						<view class="formPost_con">
							<view class="submitBtn cancelBtn" @click="delShow = false">取消</view>
							<button form-type="submit" hover-class="none" class="submitBtn continueBtn">确定</button>
						</view>
					</form>
				</view>
			</view>
		</u-mask>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				project_id: '',
				worksiteDetail: {},

				// 接收消息提醒设置
				receiveChecked: false,

				// 默认工地设置
				defaultChecked: false,

				// 删除工地弹窗
				delShow: false,
			}
		},
		onLoad(option) {
			this.project_id = option.project_id;
		},
		onShow() {},
		onReady() {
			this.getWorksiteDetail();
		},
		methods: {
			// 获取工地详情
			getWorksiteDetail() {
				var _this = this;
				_this.$u.get('/api/project.index/getDetail', {
					id: _this.project_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取工地详情==");
					console.log(res);
					_this.worksiteDetail = res.data;
					_this.defaultChecked = res.data.memberInfo.is_default == 1 ? true : false;
				}).catch(res => {
					console.log(res);
				})
			},

			// 获取当前位置
			getLocation() {
				var _this = this;
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						_this.address = res.address;
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
					}
				});
			},
			
			editSubmit(e) {
				// console.log(e);
				var _this = this;
				var editData = e.detail.value;
			
				if (!editData.name) {
					uni.showModal({
						content: '项目名称不能为空',
						showCancel: false,
					})
					return;
				}
			
				_this.editData = editData;
				_this.$u.throttle(_this.editForm, 3000);
			},
			
			editForm() {
				var _this = this;
				_this.$u.post('/api/project.index/add_do', {
					id: _this.project_id,
					name: _this.editData.name,
				}).then(res => {
					console.log(res);
					_this.editShow = false;
					uni.showToast({
						title: '编辑成功',
						duration: 2000
					});
					setTimeout(function() {
						// _this.getWorksiteDetail();
						uni.navigateBack()
					}, 2000)
				}).catch(res => {
					console.log(res);
				})
			},

			// 接收消息提醒设置
			receiveChange(status) {
				// console.log(status);
				this.receiveChecked = status;
			},

			// 设置默认
			defaultChange(status) {
				var _this = this;
				_this.defaultChecked = status;

				if (_this.worksiteDetail.history != 0) {
					uni.showModal({
						title: '提示',
						content: '请先恢复工地再进行操作',
						showCancel: false,
					})
					return;
				}
				_this.$u.get('/api/project.index/updateDefault', {
					project_id: _this.worksiteDetail.id,
				}).then(res => {
					uni.hideLoading();
					// _this.pages = 1;
					// _this.getWorksiteList();
					uni.reLaunch({
						url: '/pages/index/index'
					})

					//更新默认工地
					_this.getDefaultWorksite()
				}).catch(res => {
					console.log(res);
				})
			},

			/**
			 * 删除
			 */
			openDelShow(projectId) {
				var _this = this;
				_this.delShow = true;
			},
			delSubmit(e) {
				var _this = this;
				var delData = e.detail.value;
				if (_this.project_id != delData.checkCard) {
					uni.showModal({
						content: '验证码不对',
						showCancel: false,
					})
					return;
				}
				_this.delData = delData;
				_this.delShow = false;
				_this.$u.throttle(_this.delForm, 3000);
			},

			delForm() {
				var _this = this;
				_this.$u.post('/api/project.index/setDelete', {
					id: _this.project_id,
				}).then(res => {
					console.log("==删除==");
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				}).catch(res => {
					console.log(res);
				})
			},

		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
	.handleList_box {
		padding: 15rpx 30rpx;
	}

	.setList_con {
		padding: 0 30rpx;
	}

	.setItem {
		padding: 30rpx 0;
		border-bottom: 1rpx solid #eee;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.setItem .setIcon {
		width: 40rpx;
		height: 40rpx;
	}

	.setItem .setTit {
		flex: 2;
		font-size: 28rpx;
		font-weight: bold;
		color: $uni-color;
		margin: 0 20rpx;
	}

	// 删除工地弹窗
	.delPop_box {
		width: 100%;
		height: 100vh;
		position: relative;
		box-sizing: border-box;
	}

	.delPop_con {
		width: 550rpx;
		background: linear-gradient(180deg, #C2EBE7 0%, #F5F6F8 25%);
		border-radius: 20rpx;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		z-index: 99;
	}

	.formIcon {
		text-align: center;
		margin-top: -73rpx;
	}

	.formIcon image {
		width: 146rpx;
		height: 146rpx;
	}

	.formTit {
		font-size: 32rpx;
		font-weight: bold;
		text-align: center;
		padding: 40rpx 0;
	}

	.formWarn {
		font-size: 30rpx;
		color: #999;
		text-align: center;
	}

	.formClass {
		overflow: hidden;
	}

	.classItem {
		font-size: 30rpx;
		color: #333;
		text-align: center;
		overflow: hidden;
		margin-top: 30rpx;
		position: relative;
	}

	.classItem::before {
		content: '';
		width: 16rpx;
		height: 16rpx;
		background: $uni-color-active;
		border-radius: 50%;
		overflow: hidden;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		margin-left: -80rpx;
	}

	.formDel {
		font-size: 30rpx;
		color: #333;
		text-align: center;
		overflow: hidden;
		margin-top: 30rpx;
	}

	.delPost_con {
		padding: 0 30rpx 30rpx;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.cancelBtn {
		background: #eee;
		color: #333;
		font-weight: normal;
		color: $uni-color;
	}

	.continueBtn {
		font-weight: normal;
		color: $uni-color-white;
		background: $uni-color-active;
		margin-left: 40rpx;
	}

	.controls {
		border-radius: 20rpx;
		width: 100%;
		overflow: hidden;
		padding: 0 30rpx;
		margin: 20rpx auto;
	}

	.controls input {
		height: 90rpx;
		line-height: 20rpx;
		text-align: center;
		margin: 0;
		padding: 0;
		background-color: #fff;
		border-radius: 20rpx;
		margin-top: -4rpx;
	}
</style>
